<h1 class="right">إدارة الوحدات والتسكين</h1><!--<div class="h1-dot"></div>-->
      				  				  <?php
                  if ($_SESSION['r']<=1)
				  {
				  ?>  
<a href="#add_client" class="add_client_pop">
<img src="images/add-icon.png" align="left" class="left" alt="" title="إضافة شقة"/>
</a>

<div id="add_client" class="zoom-anim-dialog mfp-hide max-width800">
  <?php require_once("Apartments/Create.php"); ?>
</div>
<?php
}
?>
<a href="create-pop-ups/contract.php" class="ajax-pop-up-magnific-modal">
  <img src="images/add-icon.png" align="left" class="left" alt="" title="إضافة عقد"/>&nbsp;
</a>

<span class="left grey medium margin-left11 margin-top5"><?php echo date('g:ia d/m/Y')?></span>
<div class="clear"></div>
<div id="dashboard-container">
	<div class="apartment-filter">
    	<table class="bookings-filter">
        <tr>
          <form name="apartment_search" action="" method="GET" id="searchClient" class="apartment_filter">

        	<td width="60">الوحدة &nbsp;:</td>
            <td style="width:100px;">
          <select name="apart_id" class="">
            
            <?php 
            if(isset($_SESSION['user_id']))
            {
            
              if(isset($binds['id']) && $binds['id'] == -1)
                echo "<option selected = 'selected' value='-1'>الجميع</option>";
              else
                echo "<option value='-1'>الجميع</option>";
                
              $aprtes = query("SELECT apart_name,id FROM apartments WHERE owner_id=?",$_SESSION['user_id']);
              foreach($aprtes as $aprt)
              {
                
                if(isset($binds['id']) && $binds['id'] == $aprt['id'] )
                  echo "<option selected='selected' value='{$aprt['id']}'>{$aprt['apart_name']}</option>";
                else
                  echo "<option  value='{$aprt['id']}'>{$aprt['apart_name']}</option>";
                
              }
            }
            ?>
          </select>
          </td>
          <td>الحالة&nbsp;:</td>
          <td style="width:265px;">
          <select name="apart_status" class="apartment_status">
            <?php 
              if(isset($binds['status']) && $binds['status'] == -1)
                echo "<option selected = 'selected' value='-1'>الجميع</option>";
              else
                echo "<option value='-1'>الجميع</option>";
                
              $status=query("SELECT * FROM apart_status");
			  
              foreach($status as $stat)
              {
                
                if(isset($binds['status']) && $binds['status'] == $stat['status_id'] )
                  echo "<option selected='selected' value='{$stat['status_id']}'>{$stat['status_txt']}</option>";
                else
                  echo "<option value='{$stat['status_id']}'>{$stat['status_txt']}</option>";
               
               
              }
            
            ?>
          </select>
          </td>
		  <td WIDTH="80">رقم الوحدة&nbsp;:</td>
           <td style="width:50px;"><input name="room_name" type="text" class="auto-input" value="<?php if(isset($binds['room_name']) ) {echo $binds['room_name'];} ?>"/></td>
		   <td width="2"></td>
           <td width="80">عدد الغرف&nbsp;:</td>
           <td style="width:50px;"><input name="rooms#" type="text" class="auto-input" value="<?php if(isset($binds['rooms#']) ) {echo $binds['rooms#'];} ?>"/></td>
		   <td width="2"></td>
		   <td>الطابق&nbsp;:</td>
           <td style="width:50px;"><input name="floor#" type="text" class="auto-input" value="<?php if(isset($binds['floor#']) ) {echo $binds['floor#'];} ?>"/></td>
           <td style="width:10px;"></td>
           <td>النوع&nbsp;:</td>
           <td style="width:100px;">
          <select name="apart_type">
            <?php 
              if(isset($binds['apart_type']) && $binds['apart_type'] == -1)
                echo "<option selected = 'selected' value='-1'>الجميع</option>";
              else
                echo "<option value='-1'>الجميع</option>";
            
              
               
              foreach($types as $type)
              {
                if(isset($binds['apart_type']) && $binds['apart_type'] == $type['type_id'] )
                  echo "<option selected = 'selected' value='{$type['type_id']}'>{$type['type_name']}</option>";
                else
                  echo "<option value='{$type['type_id']}'>{$type['type_name']}</option>";
               
               
              }
            
            ?>
          </select>
         </td >
            <td style="width:40px;"><a href="#"><div class="search-botton">بحث</div></a></td>
        </form>
       
       </tr>
        </table>
    </div>
    
    
      <div class="dashboard-row">
    	<?php 
      
      if(!empty($aprts))
      {
        foreach($aprts as $aprt)
        {
      ?>
      
      <?php 
          if($aprt['status'] == 7 ){ ?>
           <!--<a href="#">--><div class="apartments-cell relative cell-parent"><img src="images/home-<?php echo $aprt['status']; ?>.png" class="" align="middle" alt="" title="<?php echo $aprt['apart_desc']; ?>"/><!--</a>-->
            
      <?php }else if($aprt['status'] >= 2 && $aprt['status'] <= 4 ){ ?>      
            <!--<a href="create-pop-ups/contract.php?id=<?php //echo $aprt['apartment__id'];?>&name=<?php //echo $aprt['apart_name'];?>" class="ajax-pop-ups-magnific-no-modal">--><div class="apartments-cell relative cell-parent"><img src="images/home-<?php echo $aprt['status']; ?>.png" class="" align="middle" alt="" title="<?php echo $aprt['apart_desc']; ?>"/><!--</a>-->
            
       <?php }else{ ?>            
            <a href="edit-pop-ups/contract.php?id=<?php echo $aprt['contract_id'] ?>" class="ajax-pop-up-magnific-modal"><div class="apartments-cell relative cell-parent"><img src="images/home-<?php echo $aprt['status']; ?>.png" class="" align="middle" alt=""/></a>
      <?php } ?>
            <p class="apartment-oName"><?php echo $aprt['apart_name']; ?></p>
			 <?php
			  if($aprt['status'] == 8){
			  $sql=query("SELECT * FROM requests WHERE apart_name = ? ",$aprt["apart_name"]);
			  $date_beginning= strtotime($sql[0]["date_beginning"]);
			  ?>
			  <p class="apartment-oName" id="day_in"><?php echo date("Y-m-d",$date_beginning); ?></p>
			     
			  <?php } ?>
      <?php 
	  
        if($aprt['status'] == 1 || $aprt['status'] == 5 || $aprt['status'] == 6){
          $from =  strtotime($aprt['mildai_from']);
          $to =  strtotime($aprt['miladi_out']);
      ?>
            <p class="apartment-oName" id="day_in"><?php echo date("Y-m-d",$from); ?></p>
            <p class="apartment-oName" id="day_out"><?php echo date("Y-m-d",$to); ?></p>
      <?php } ?>
            <div class="absolute apartment_details">
              
              <?php
                if($aprt['status'] == 1 || $aprt['status'] == 5 || $aprt['status'] == 6)
                {
                  $price_discounted = $aprt['total'] - ( ( ($aprt['discount'] + $aprt['co_discount']) )  ) - $aprt['fixed_discount'];
                  if( round($aprt['paid'] - $aprt['paid_back'] - $price_discounted - $aprt['services_revenue'],2) > 0 )
                  {
                    echo "<p class='apartment-credit green'>";
                    echo "الرصيد: ".round(($aprt['paid'] - $aprt['paid_back'] - $price_discounted - $aprt['services_revenue']),2);
                    echo "</p>";
                  }
                  else if(round($aprt['paid'] - $aprt['paid_back'] - $price_discounted - $aprt['services_revenue'],2) < 0) 
                  {
                    echo "<p class='apartment-credit red'>";
                    echo "الرصيد: ". round(($aprt['paid'] - $aprt['paid_back'] - $price_discounted - $aprt['services_revenue']),2);
                    echo "</p>";
                  }
                  else
                  {
                    echo "<p class='apartment-credit'>الرصيد: صفر</p>";
                  }
                }
              ?>
             
              <div class="apartment-menu">
                  <!-- View pop-up triggerer, Ajax request will be sent to href url -->
				  <?php
				  if ($_SESSION['r']<=2)
				  {
				  ?>
                  <a href="view-pop-ups/apartment.php?id=<?php echo $aprt['apartment__id']; ?>" class="ajax-pop-up-magnific-modal"><img src="images/view.png" class="right" align="" alt="View"/></a>					
				  <a href="edit-pop-ups/apartment.php?id=<?php echo $aprt['apartment__id']; ?>&  page= <?php echo $page;?>" class="ajax-pop-up-magnific-modal"><img src="images/edit.png" class="right margin-right5" align="" alt="Edit"/></a>
				<?php
				  }
				  elseif($_SESSION['r']==4)
				  {
				  ?>
				<a href="edit-pop-ups/apartment.php?id=<?php echo $aprt['apartment__id']; ?>" class="ajax-pop-up-magnific-modal"><img src="images/edit.png" class="right margin-right5" align="" alt="Edit"/></a>	
				<?php
				}
				?>
      				  				<?php
										if ($_SESSION['r']<=1)
										{
									?>            
				  
                  <!-- Edit pop-up triggerer, Ajax request will be sent to href url -->
                  
                  <a href="#" class="disable"><img src="images/lock.png" class="right margin-right5" align="" alt="Lock"/></a>
				  <input class="disable-id" type="hidden" value="<?php echo $aprt['apartment__id'] ?>"/>
				  <input class="pages" type="hidden" value="<?php echo $page; ?>"/>
                  <input name="disable" class="disable" type="hidden"  value="20"/> <!-- 20 means disable apartments -->    

				  <a href="#" class="delete"><img src="images/x.png" class="right margin-right5" align="" alt="Delete"/></a>
                  <input class="delete-id" type="hidden" value="<?php echo $aprt['apartment__id'] ?>"/>
                  <input class="filter-id" name="filter" type="hidden" value="1"/> <!-- 1 means delete apartments -->

									<?php
										}
									?>
				  
              </div>
            </div>
        </div>
      <?php 
        }
      }
      ?>
        

    </div>
    
        <?php
          
          $start = $page * 20 -19;
          $end  = $start + 19;
          if($end > $totalPages)
            $end = $totalPages;
          
          if(!empty($aprts))
          {
        ?>
        <div class="clear"></div>
        <div class="relative">
        <div id="apartments-selection"></div>
        <div class="xsmall showing-results absolute">عرض النتائج <?php echo $start; ?> - <?php echo $end; ?> من إجمالى <?php echo $totalPages; ?></div>
        </div> 
        <?php } ?>
        <table class="apartments-status">
        	<tr>
                <td><a href="#" class="black xsmall"><img src="images/home-1-s.png" class="margin-side-5 image-filter" align="" alt="" value="1"/>مؤجر [<?php echo $M[0]['COUNT(*)'];  ?>]</a></td>
                <td><a href="#" class="black xsmall"><img src="images/home-2-s.png" class="margin-side-5 image-filter" align="" alt="" value="2"/>فارغ [<?php  echo $F[0]['COUNT(*)'];  ?>]</a></td>
                <td><a href="#" class="black xsmall"><img src="images/home-3-s.png" class="margin-side-5 image-filter" align="" alt="" value="3"/>تنظيف [<?php echo $T[0]['COUNT(*)'];  ?>]</a></td>
                <td><a href="#" class="black xsmall"><img src="images/home-4-s.png" class="margin-side-5 image-filter" align="" alt="" value="4"/>صيانة [<?php echo $S[0]['COUNT(*)'];  ?>]</a></td>
				<td><a href="#" class="black xsmall"><img src="images/home-8-s.png" class="margin-side-5 image-filter" align="" alt="" value="8"/>طلب تنظيف [<?php echo $R[0]['COUNT(*)'];  ?>]</a></td>
                <td><a href="#" class="black xsmall"><img src="images/home-5-s.png" class="margin-side-5 image-filter" align="" alt="" value="5"/>العقود المنتهية اليوم [<?php echo $A[0]['COUNT(*)'];  ?>]</a></td>
                <td><a href="#" class="black xsmall"><img src="images/home-6-s.png" class="margin-side-5 image-filter" align="" alt="" value="6"/>محجوز بدون دخول [<?php echo $B[0]['COUNT(*)'];  ?>]</a></td>
				<td><a href="#" class="black xsmall"><img src="images/home-9-s.png" class="margin-side-5 image-filter" align="" alt="" value="6"/>موعد دخولها اليوم [<?php echo $B[0]['COUNT(*)'];  ?>]</a></td>
				<?php  if($N[0]['COUNT(*)'] != 0){
		              	$RATE=( ($M[0]['COUNT(*)']+$R[0]['COUNT(*)']+$A[0]['COUNT(*)'])/$N[0]['COUNT(*)'])*100; 
			   ?>
                <td style=""> 	نسبة الإشغال [<?php echo round($RATE, 2);   ?>]%</td>
				<?php } ?>
            </tr>
        </table>
        <div class="hidden totalPages"><?php if(isset($totalPages)) {echo $totalPages;} ?></div>
        <div class="hidden currentPage"><?php if(isset($page)) {echo $page;} ?></div>
        <!-- change the value of requsting-page to indicate that the submission came from apartments page -->
        <script>
          $('.requsting-page').val(1);
        </script>

</div>